package com.zjq.authoritysystem.controller.role;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjq.authoritysystem.dto.RolePermissionDto;
import com.zjq.authoritysystem.entity.Role;
import com.zjq.authoritysystem.entity.User;
import com.zjq.authoritysystem.service.PermissionService;
import com.zjq.authoritysystem.service.RoleService;
import com.zjq.authoritysystem.vo.RolePermissionVo;
import com.zjq.authoritysystem.vo.query.RoleQueryVo;
import com.zjq.authoritysystem.vo.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author BetOnMe
 * @version 1.0
 * @ClassName RoleController
 * @description: TODO
 * @since 2023年09月18日 22:35:29
 */
@Slf4j
@Api(tags = "角色控制器")
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @ApiOperation(value = "分页查询角色信息")
    @PostMapping("/list")
    public Result list(@RequestBody RoleQueryVo roleQueryVo) {
        Page roles = roleService.listByPage(roleQueryVo);
        return Result.SUCCESS(roles);
    }

    @ApiOperation(value = "根据编号查询角色信息")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Integer id) {
        Role role = roleService.getById(id);
        return Result.SUCCESS(role);
    }

    @ApiOperation(value = "保存角色信息")
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        role.setCreatorId(user.getId());
        List<Role> roles = roleService.getByCode(role.getCode());
        if (role.getId() == null && roles.size() > 0) {
            return Result.Error("该角色编号已存在，请更换角色编码");
        }
        boolean saved = roleService.saveOrUpdate(role);
        if (!saved) {
            return Result.Error("保存失败");
        }
        return Result.SUCCESS();
    }

    @ApiOperation(value = "删除角色信息")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean deleted = roleService.delete(id);
        if (!deleted) {
            return Result.Error("删除失败！");
        }
        return Result.SUCCESS();
    }

    @ApiOperation(value = "分配权限，根据用户编号和角色编号获取权限树信息")
    @GetMapping("/getAssignPermissionsTree/{roleId}")
    public Result getAssignPermissionsTree(@PathVariable Integer roleId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        RolePermissionVo permissionTree = permissionService.findPermissionTree(user.getId(), roleId);
        return Result.SUCCESS(permissionTree);
    }

    @ApiOperation(value = "保存角色权限信息")
    @PostMapping("saveRolePermissions")
    public Result saveRolePermissions(@RequestBody RolePermissionDto rolePermissionDto) {
        boolean saved = roleService.saveRolePermissions(rolePermissionDto);
        if (!saved) {
            return Result.Error("分配角色权限失败");
        }
        return Result.SUCCESS("分配角色权限成功");
    }

}
